﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZSPG_ZSPersonControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {

            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Bind();
            }
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            CustomGridView1.RowDeleting += new GridViewDeleteEventHandler(CustomGridView1_RowDeleting);
            Button btnsearch = (Button)FindControl("btnSeach");
            btnsearch.Click += new EventHandler(Go_Search);
            Button btnAdd = (Button)FindControl("btnAdd");
            btnAdd.Click += new EventHandler(btnAdd_Click);
            IsShiPerson();

        }


        private void IsShiPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + @"') And(Rolename Like '%市征收%' Or Rolename Like '%超级管理员%') ";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                if (userid != "6506D49F48F9489F9FE921501CF767B0")
                {
                    Button btnAdd = (Button)FindControl("btnAdd");
                    btnAdd.Enabled = false;
                }
            }
        }

        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ZSPG_ZSPersonInfo.aspx");
        }
        protected void Bind()
        {
            if (Page.Session["UserID"] != null)
            {
                userid = Page.Session["UserID"].ToString();
            }
            string sql1 = @"Select Rolename
  From Zs_Role Where Id in (Select Roleid  From Zs_User_In_Role Where Userid = '" + userid + "')";
            DataTable Rname = _EluZSDataProvider.GetTable(sql1);
            string rolename = string.Empty;
            string sql = string.Empty;
            for (int i = 0; i < Rname.Rows.Count; i++)
            {
                if (Rname.Rows[i]["Rolename"].ToString().Equals("超级管理员"))
                {
                    sql = "select ID,项目名称 from zs_project where 1=1 ";
                }
                string quname = Rname.Rows[i]["Rolename"].ToString().Substring(0, 3);
                if (quname.Equals("玄武区") || quname.Equals("江宁区") || quname.Equals("六合区") || quname.Equals("溧水县") || quname.Equals("高淳县") || quname.Equals("鼓楼区") || quname.Equals("白下区") || quname.Equals("建业区") || quname.Equals("秦淮区") || quname.Equals("下关区") || quname.Equals("栖霞区") || quname.Equals("雨花台区") || quname.Equals("浦口区"))
                {
                    sql = "select ID,项目名称 from zs_project where 1=1 and 区属 like'%" + rolename + "%' ";
                }
                if (quname.Equals("市征收"))
                {
                    sql = "select ID,项目名称 from zs_project where 1=1 ";
                }
            }

            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.RowDataBound += new GridViewRowEventHandler(CustomGridView1_RowDataBound);
            DataTable dt = GetBZSR(GetStrWhere(), rolename);//获取人员数据
            DataColumn dc = new DataColumn("编辑");
            dt.Columns.Add(dc);
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.PageSize = 10;
            CustomGridView1.DataKeyNames = new string[] { "ID" };

            CustomGridView1.DataBind();

        }


        protected DataTable GetBZSR(string strWhere, string rolename)
        {
            string nameStr = "Select rolename  From zs_role  Where Id In (Select roleid From zs_user_in_role Where userid = '"+userid+"')";
            string wheres = string.Empty;
            DataTable dt = _EluZSDataProvider.GetTable(nameStr);
            if (dt.Rows.Count>0)
            {
                if (dt.Rows[0]["rolename"].ToString() != "超级管理员")
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string rolenames = dt.Rows[i]["rolename"].ToString().Substring(0, 2);
                        if (rolenames == "鼓楼" || rolenames == "溧水" || rolenames == "高淳" || rolenames == "六合" || rolenames == "浦口" || rolenames == "建邺" || rolenames == "秦淮" || rolenames == "栖霞" || rolenames == "雨花" )
                        {
                            wheres = " And t.pid In (Select Id From zs_project Where zs_project.区属 Like '%" + rolenames + "%')";
                        }
                        
                    }
                }
            }
            TextBox txt项目名称 = (TextBox)FindControl("txt项目名称");
            string nwhere = string.Empty;
            if (!string.IsNullOrEmpty(rolename))
            {
                nwhere += " And t.pid In (Select Id From zs_project Where  区属 Like '%" + rolename + "%') ";
            }
            if (!string.IsNullOrEmpty(txt项目名称.Text.Trim()))
            {
                nwhere += " And t.pid In (Select Id From zs_project Where  项目名称 Like '%" + txt项目名称.Text+ "%') ";
            }
            string sql = @"select t.id,
(Select 项目名称 from zs_project p where p.id in t.pid ) 项目名称,产权人,证件号,丘权号,权号,房屋坐落,房屋建筑面积 面积,承租人,(select keyname from zs_params where name='国有土地使用证证载用途' and keyvalue=to_number(nvl(t.房屋用途,0))) 房屋产权证证载用途,(select keyname from zs_params where name='结构等级' and keyvalue=to_number(nvl(t.房屋结构,0))) 房屋结构,使用性质,所属层次,房屋权证编号,土地权证编号,土地分摊面积,征收区位等级,所属类别,使用面积,(select keyname from zs_params where name='证件类型' and keyvalue=to_number(nvl(t.证件类型,0))) 证件类型 from zs_zsperson t where 1=1  and is_number(房屋结构)='Y'" + nwhere + @" ";
            
            if (!string.IsNullOrEmpty(strWhere))
            {
                sql += strWhere;
            }
            if (!string.IsNullOrEmpty(wheres))
            {
                sql += wheres;
            }



            sql += " Order By t.ADDTIME desc";
            return _EluZSDataProvider.GetTable(sql);
        }

        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void CustomGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            string personid = CustomGridView1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("zs_zsperson", "id", personid);
            if (del)
            {
                Bind();
                HttpContext.Current.Response.Write("<script>alert('删除成功！');window.location.href=location.href;</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
            }
        }
        protected string GetStrWhere()
        {
            string strwhere = string.Empty;
            TextBox txtPerName = (TextBox)FindControl("txtPerName");
            TextBox txtFWZL = (TextBox)FindControl("txtFWZL");
            //DropDownList ddl项目名称 = (DropDownList)FindControl("ddl项目名称");
            
            string perName = txtPerName.Text.Trim();
            string perfwzl = txtFWZL.Text.Trim();
            if (!string.IsNullOrEmpty(perName))
            {
                strwhere += " And (产权人 Like '%" + perName + "%' Or 承租人 Like '%" + perName + "%')";
            }
            if (!string.IsNullOrEmpty(perfwzl))
            {
                strwhere += " and 房屋坐落 like '%" + perfwzl + "%'";
            }
            
            return strwhere;
        }
        protected void CustomGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string strID = rowview["ID"].ToString();
                bool isview = _EluZSDataProvider.GetBZSRXY(strID);
                if (isview)
                {
                    e.Row.Cells[11].Text = "查看";
                    e.Row.Cells[11].Attributes.Add("style", "cursor:pointer;color:#223399;");
                    e.Row.Cells[11].Attributes.Add("onclick", "javascript:window.location.href=('ZSPG_ZSPersonInfo.aspx?ID=" + strID + "');");
                    e.Row.Cells[13].Text = "删除";
                    e.Row.Cells[13].Attributes.Add("style", "cursor:pointer;color:gray;");
                    e.Row.Cells[13].Attributes.Add("onclick", "javascript:alert('此被征收人员数据已做协议维护！');");
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        e.Row.Cells[i].BackColor = System.Drawing.Color.FromName("blue");
                    }
                }


            }


        }
    }
}


